<?
	class UserSession
	{
		var $cDBUser;
		var $cDBPassword;
		var $ncUsuario;
		var $cRol;

	function UserSession ()
		{
			// Setea los valores cuando no esta logeado
			$this->cDBUser = DB_USER;
			$this->cDBPassword = DB_PASSWORD;
			// Si esta logeado => extrae los parametros
			if ( $_SESSION[USER_SESSION_VARIABLE_NAME] )
			{
				$cUser = $this->decrypt($_SESSION[USER_SESSION_VARIABLE_NAME], session_id() );
				$aUser = explode( "|", $cUser ) ;
				$this->ncUsuario 			= $aUser[0];
				$this->cDBUser 			= $aUser[1];
				$this->cDBPassword 	= $aUser[2];
				$this->cRol 				= strtolower($aUser[3]);
			}
		}
	function crypt( $cText, $cKey )
		{
			$nLong = strlen($cText);
			$cReturn  = '';

			for ( $nIndex = 0; $nIndex < $nLong; $nIndex++ ) {
				$nSuma = substr( $cKey, ($nIndex % strlen($cKey)), 1) ;
				$nNuevo = ((ord( substr($cText, $nIndex, 1) ) + $nSuma )  % 256 ) ^ 171;
				$cReturn .= chr($nNuevo);
			}
			return $cReturn;
		}
	function decrypt( $cText, $cKey )
		{
			$nLong = strlen($cText);
			$cReturn  = '';

			for ( $nIndex = 0; $nIndex < $nLong; $nIndex++ ) {
				$nSuma = substr( $cKey, ($nIndex % strlen($cKey)), 1) ;
				$nNuevo = (( 256 + (ord( substr($cText, $nIndex, 1)) ^ 171) - $nSuma )  % 256 );
				$cReturn .=chr($nNuevo);
			}
			return $cReturn;
		}
	function login( $ncUsuario , $cUsuario, $cClave, $cRoles)
		{
			$cUser = $this->crypt( $ncUsuario . '|' .$cUsuario. '|' . $cClave . '|' . $cRoles , session_id());
			$_SESSION[USER_SESSION_VARIABLE_NAME] = $cUser;
			$this->ncUsuario 		= $ncUsuario;
			$this->cDBUser 			= $cUsuario;
			$this->cDBPassword 		= $cClave;
			$this->cRol 			= $cRoles;
		}
	function logout ()
		{
			$_SESSION[USER_SESSION_VARIABLE_NAME] = '';
		}
	function getUser()
		{
			return $this->ncUsuario;
		}
	function getDBUser()
		{
			return $this->cDBUser;
		}
	function getDBPassword()
		{
			return $this->cDBPassword;
		}
		// Devuelve True si ya esta logeado
	function isLogged()
		{
			return $this->ncUsuario ? true : false;
		}
	function getRolArray( )
	{
		foreach( explode( ';', $this->cRol) as $cRol)
		{
			list ( $cModule , $cRoles ) = explode( ':', $cRol ) ;
			$aRoles[$cModule] .= $cRoles; 
		}
		return $aRoles;
	}
	function getRol( $cModule )
		{
			//Si tiene un Rol Multimodules 
			$nPosDesde = strpos ( $this->cRol, "all:" );
			if ( !($nPosDesde === FALSE) )
			{
				$nPosHasta  = strpos ( $this->cRol, ";", $nPosDesde );
				if (  !$nPosHasta === FALSE)  $cRolAll =  substr( $this->cRol, $nPosDesde +  4 , $nPosHasta  - $nPosDesde  - 4 ) ;
			}
			//
			$cModule = strtolower($cModule);
			$nPosDesde = strpos ( $this->cRol, $cModule . ":" );
			$nPosHasta  = strpos ( $this->cRol, ";", $nPosDesde );
			$nLength = strlen($cModule) + 1;
			if ( $nPosHasta === FALSE ) $nPosHasta = strlen($this->cRol);
			$cRolModule= ( $nPosDesde === FALSE || $nPosHasta === FALSE)  ?  '': substr( $this->cRol, $nPosDesde +  $nLength , $nPosHasta  - $nPosDesde  - $nLength ) ;
			return $cRolModule . ($cRolAll  ? ','. $cRolAll  : '' );
		}
	}
?>